package com.xbfinance.manager.web;

import com.xbfinance.manager.pojo.po.P2pUser;
import com.xbfinance.manager.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @GetMapping("/name/{username}")
    @ResponseBody
    public Map<String, Object> isNameExist(@PathVariable String username){
        Map<String, Object> existFlag = new HashMap<>();
        boolean flag=userService.isNameExist(username);
        existFlag.put("flag",flag);
        return existFlag;
    }
    @GetMapping("/email")
    @ResponseBody
    public Map<String, Object> isEmailExist(String email){
        Map<String, Object> existFlag = new HashMap<>();
        boolean flag=userService.isEmailExist(email);
        existFlag.put("flag",flag);
        return existFlag;
    }
    @PostMapping("/regist")
    public String regist(P2pUser p2pUser, Model model){
        try {
            p2pUser=userService.regist(p2pUser);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if(p2pUser!=null){
            model.addAttribute("userid", p2pUser.getUserid());
            model.addAttribute("username", p2pUser.getUsername());
            model.addAttribute("phone", p2pUser.getPhone());
            model.addAttribute("email", p2pUser.getEmail());
            return "regist_yz_mail";
        }
        model.addAttribute("errorMsg", "注册失败");
        return "regist";
    }
    @PostMapping("/smrz")
    public String userinfo(String realname,String cardnum,Integer userid,Model model){
        boolean flag=false;
        try {
            flag=userService.putInfo(realname,cardnum,userid);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (flag) {
            return "index";
        }else{
            model.addAttribute("userid",userid);
            model.addAttribute("errorMsg","实名认证错误");
            return "regist_success";
        }
    }
}
